package com.tycmc.signelectroniccontracts.act.renzheng;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import com.baidu.ocr.sdk.model.IDCardParams;
import com.baidu.ocr.ui.camera.CameraActivity;
import com.tycmc.signelectroniccontracts.R;
import com.tycmc.signelectroniccontracts.logic.renzheng.PrePeopleRenzhen;
import com.tycmc.signelectroniccontracts.tools.base.ActBase;
import com.tycmc.signelectroniccontracts.tools.images.Bimp;
import com.tycmc.signelectroniccontracts.tools.images.ImageSetting;
import com.tycmc.signelectroniccontracts.tools.images.UrlPath;
import com.tycmc.signelectroniccontracts.tools.other.TextShow;
import com.tycmc.signelectroniccontracts.tools.other.TyTypenum;
import com.tycmc.signelectroniccontracts.tools.permissions.PermissionsTool;
import com.tycmc.signelectroniccontracts.tools.show.ToastShow;
import com.tycmc.signelectroniccontracts.tools.store.SharePre;
import com.tycmc.signelectroniccontracts.tools.store.SharePreName;
import com.tycmc.signelectroniccontracts.tools.times.DataTimer;
import com.tycmc.signelectroniccontracts.ui.PopBottom;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ActPeopleRenzhen extends ActBase implements PrePeopleRenzhen.PeopleRenZhenInterface {

    private PrePeopleRenzhen pre;
    private String filePath;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_people_renzhen);
        filePath = System.currentTimeMillis() + "";
        pre = new PrePeopleRenzhen(this, getIntent(), filePath);

        initView();
        pre.getInfo();
        setTitleText("个人身份认证");
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        pre.onResult(requestCode, resultCode, data);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        pre.destory();
    }

    private void initView() {
        TextShow.setTextNeed((TextView) findViewById(R.id.tv_xingming_state));
        TextShow.setTextNeed((TextView) findViewById(R.id.tv_sex_state));
        TextShow.setTextNeed((TextView) findViewById(R.id.tv_mingzu_state));
        TextShow.setTextNeed((TextView) findViewById(R.id.tv_chushenriqi_state));
        TextShow.setTextNeed((TextView) findViewById(R.id.tv_shenfenzhenghao_state));
        TextShow.setTextNeed((TextView) findViewById(R.id.tv_zhuzhi_state));
        findViewById(R.id.tv_chushenriqi_state).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DataTimer.choseDay(ActPeopleRenzhen.this, new DataTimer.TimerCallBack() {
                    @Override
                    public void callBack(String time) {
                        ((TextView) findViewById(R.id.tv_chushenriqi)).setText(time);
                    }
                }, ((TextView) findViewById(R.id.tv_chushenriqi)).getText().toString().trim(), "", "");
            }
        });
        findViewById(R.id.tv_sex_state).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PopBottom popBottom = new PopBottom(ActPeopleRenzhen.this, new PopBottom.OnItemListen() {
                    @Override
                    public void itemOnclick(AdapterView<?> parent, int position, View view) {
                        if (parent.getAdapter().getItem(position) instanceof Map) {
                            Map item = (Map) parent.getAdapter().getItem(position);
                            ((TextView) findViewById(R.id.tv_sex)).setText(item.get(PopBottom.NAME) + "");
                        }
                    }
                });
                List<Map> list = new ArrayList<>();
                Map map = new HashMap();
                map.put(PopBottom.NAME, "男");
                Map map1 = new HashMap();
                map1.put(PopBottom.NAME, "女");
                list.add(map);
                list.add(map1);
                popBottom.addAll(list);
                popBottom.show(v);
            }
        });
        //获取验证码
        findViewById(R.id.tv_getcode).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                pre.getcode();
            }
        });
        findViewById(R.id.ll_add_before).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                PermissionsTool.requestPermiss(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA}, new PermissionsTool.PerMissionCallBack() {
                    @Override
                    public void permissIsPass(String[] permission, boolean isPass) {
                        if (isPass) {
                            UrlPath.init();
                            Intent intent = new Intent(ActPeopleRenzhen.this, CameraActivity.class);
                            intent.putExtra(CameraActivity.KEY_OUTPUT_FILE_PATH,
                                    UrlPath.imagePath + IDCardParams.ID_CARD_SIDE_FRONT + filePath + ".png");
                            intent.putExtra(CameraActivity.KEY_CONTENT_TYPE, CameraActivity.CONTENT_TYPE_ID_CARD_FRONT);
                            startActivityForResult(intent, TyTypenum.renlianTouxiang);
                        } else {
                            if (permission[0].equals(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                                ToastShow.popupToast("请开启文件存储权限");
                            } else if (permission[0].equals(Manifest.permission.CAMERA)) {
                                ToastShow.popupToast("请开启照相机权限");
                            }
                        }

                    }
                }, ActPeopleRenzhen.this);

            }
        });
        findViewById(R.id.ll_add_end).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                PermissionsTool.requestPermiss(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA}, new PermissionsTool.PerMissionCallBack() {
                    @Override
                    public void permissIsPass(String[] permission, boolean isPass) {
                        if (isPass) {
                            UrlPath.init();
                            Intent intent = new Intent(ActPeopleRenzhen.this, CameraActivity.class);
                            intent.putExtra(CameraActivity.KEY_OUTPUT_FILE_PATH,
                                    UrlPath.imagePath + IDCardParams.ID_CARD_SIDE_BACK + filePath + ".png");
                            intent.putExtra(CameraActivity.KEY_CONTENT_TYPE, CameraActivity.CONTENT_TYPE_ID_CARD_BACK);
                            startActivityForResult(intent, TyTypenum.renlianBack);
                        } else {
                            if (permission[0].equals(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                                ToastShow.popupToast("请开启文件存储权限");
                            } else if (permission[0].equals(Manifest.permission.CAMERA)) {
                                ToastShow.popupToast("请开启照相机权限");
                            }
                        }

                    }
                }, ActPeopleRenzhen.this);

            }
        });

        findViewById(R.id.tv_next).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Map upMap = getUpMap();
                if (upMap == null) {
                    return;
                }
                TextView textView = (TextView) findViewById(R.id.et_code);
                String trim = textView.getText().toString().trim();
                if (TextShow.isEmpty(trim)) {
                    ToastShow.popupToast(getString(R.string.please_input_yanzhengma));
                    return;
                }
                upMap.put("verificationCode", trim);
                pre.upData(upMap);
            }
        });
    }


    @Override
    public void sendPicterBefore(String before) {
        if (new File(before).exists()) {
            findViewById(R.id.ll_add_before).setVisibility(View.GONE);
            findViewById(R.id.iv_before).setVisibility(View.VISIBLE);
            ImageView imageView = (ImageView) findViewById(R.id.iv_before);
            imageView.setTag(before);
            ImageSetting.urlImage(before, imageView);
        } else {
            findViewById(R.id.iv_before).setTag(null);
            findViewById(R.id.ll_add_before).setVisibility(View.VISIBLE);
            findViewById(R.id.iv_before).setVisibility(View.GONE);
        }
    }

    @Override
    public void sendPicterEnd(String end) {
        if (new File(end).exists()) {
            findViewById(R.id.ll_add_end).setVisibility(View.GONE);
            findViewById(R.id.iv_end).setVisibility(View.VISIBLE);
            ImageView imageView = (ImageView) findViewById(R.id.iv_end);
            imageView.setTag(end);
            ImageSetting.urlImage(end, imageView);
        } else {
            findViewById(R.id.iv_end).setTag(null);
            findViewById(R.id.ll_add_end).setVisibility(View.VISIBLE);
            findViewById(R.id.iv_end).setVisibility(View.GONE);
        }
    }

    @Override
    public void edCode(int number) {
        TextView teCode = (TextView) findViewById(R.id.tv_getcode);
        if (number > 0) {
            teCode.setText(String.format(getString(R.string.replay_getcode), number + ""));
            teCode.setEnabled(false);
        } else {
            pre.destory();
            teCode.setEnabled(true);
            teCode.setText(R.string.repleycode);
        }
    }

    @Override
    public void shoujihao(String modile) {
        if (modile.length() > 7) {
            ((TextView) findViewById(R.id.tv_dairenzhen)).setText("待认证：" + modile.substring(0, 3) + "****" + modile.substring(modile.length() - 4));
        } else {
            ((TextView) findViewById(R.id.tv_dairenzhen)).setText("待认证：****");
        }

    }

    @Override
    public void initData(String clientName, String sex, String nation, String dateOfBirth, String idcNo, String address) {
        ((TextView) findViewById(R.id.et_xingming)).setText(clientName);
        ((TextView) findViewById(R.id.tv_sex)).setText(sex);
        ((TextView) findViewById(R.id.et_mingzu)).setText(nation);
        ((TextView) findViewById(R.id.tv_chushenriqi)).setText(dateOfBirth);
        ((TextView) findViewById(R.id.et_shenfenzhenghao)).setText(idcNo);
        ((TextView) findViewById(R.id.et_zhuzhi)).setText(address);
    }

    @Override
    public void sendRenlianshibie(String clientName, String idcNo) {
        if(TextShow.isEmpty(clientName)||TextShow.isEmpty(idcNo)){
            return;
        }
        pre.huotijiance(clientName, idcNo);
    }

    public Map getUpMap() {
        HashMap map = new HashMap();
        String xingming = ((TextView) findViewById(R.id.et_xingming)).getText().toString().trim();
        String sex = ((TextView) findViewById(R.id.tv_sex)).getText().toString().trim();
        String mingzu = ((TextView) findViewById(R.id.et_mingzu)).getText().toString().trim();
        String chushenriqi = ((TextView) findViewById(R.id.tv_chushenriqi)).getText().toString().trim();
        String shenfenzhenghao = ((TextView) findViewById(R.id.et_shenfenzhenghao)).getText().toString().trim();
        String zhuzhi = ((TextView) findViewById(R.id.et_zhuzhi)).getText().toString().trim();
        if (findViewById(R.id.iv_before).getTag() != null) {
            try {
                map.put("fileBefore", new File(   Bimp.getData(findViewById(R.id.iv_before).getTag() + "")));


            } catch (IOException e) {
                map.put("fileBefore", new File(findViewById(R.id.iv_before).getTag() + ""));
                e.printStackTrace();
            }

        } else {
            ToastShow.popupToast("请拍照身份证正面");
            return null;
        }
        if (findViewById(R.id.iv_end).getTag() != null) {
            try {
                map.put("backFile", new File(   Bimp.getData(findViewById(R.id.iv_end).getTag() + "")));


            } catch (IOException e) {
                map.put("backFile", new File(findViewById(R.id.iv_end).getTag() + ""));
                e.printStackTrace();
            }

        } else {
            ToastShow.popupToast("请拍照身份证背面");
            return null;
        }
        if (TextShow.isEmpty(xingming)) {
            ToastShow.popupToast("请输入姓名");
            return null;
        }
        if (TextShow.isEmpty(sex)) {
            ToastShow.popupToast("请选择性别");
            return null;
        }
        if (TextShow.isEmpty(mingzu)) {
            ToastShow.popupToast("请输入民族");
            return null;
        }
        if (TextShow.isEmpty(chushenriqi)) {
            ToastShow.popupToast("请选择出生日期");
            return null;
        }
        if (TextShow.isEmpty(shenfenzhenghao)) {
            ToastShow.popupToast("请输入身份证号");
            return null;
        }
        if (TextShow.isEmpty(zhuzhi)) {
            ToastShow.popupToast("请输入住址");
            return null;
        }

        map.put("clientName", xingming);
        map.put("sex", sex);
        map.put("nation", mingzu);
        map.put("dateOfBirth", chushenriqi);
        map.put("idcNo", shenfenzhenghao);
        map.put("address", zhuzhi);
        map.put("clientArchiveNo", SharePre.getString(SharePreName.clientArchiveNo, ""));
        return map;
    }
}
